﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DataSync.Proc.Reader
{
    public abstract class DataSyncReaderBase : IDataSyncReader
    {
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected IDataSyncWriter CurWriter;

        public void Init(XElement xmlnode, IDataSyncWriter writer)
        {
            this.CurWriter = writer;
            if (writer == null)
            {
                throw new ApplicationException("writer不能为空");
            }
            this.ReadConfig(xmlnode);
            log.DebugFormat("Reader 初始化完成");
        }

        protected virtual void ReadConfig(XElement xmlnode)
        { }

        public virtual void Close()
        {

        }

        protected virtual void Post()
        {

        }

        protected virtual void Prepare()
        {

        }

        protected virtual int ReadData()
        {
            return 0;
        }

        public virtual int StartRead()
        {
            //预执行
            this.Prepare();
            this.CurWriter.PreExecute();
            //执行
            var num = this.ReadData();
            //后执行
            this.Post();
            this.CurWriter.PostExecute();
            return num;
        }
    }
}
